#include "Acceptor.h"
#include "Command.h"

#include <QObject>

using std::auto_ptr;
using boost::shared_ptr;

Acceptor::Acceptor()
{
}

void Acceptor::accept(std::auto_ptr<Command> cmd)
{
	if (cmd->useful())
	{
		qDebug("Executing non-transitive %s.", cmd->name());

		cmd->execute();

		_undo.push(CommandPtr(cmd.release()));

		notifyObservers();
	}
	else
	{
		qDebug("Unxeceuting useless %s.", cmd->name());
		cmd->unexecute();  // unexecute any transitive executions
	}
}

void Acceptor::acceptTransitive(Command& cmd)
{
	qDebug("Executing transitive %s.", cmd.name());

	cmd.execute();
}

bool Acceptor::canUndo() const
{
	return !_undo.empty();
}

void Acceptor::undo()
{
	CommandPtr cmd = _undo.top();

	cmd->unexecute();

	// Update the stacks.
	_undo.pop();
	_redo.push(cmd);

	qDebug("Undid %s", cmd->name());

	notifyObservers();
}

bool Acceptor::canRedo() const
{
	return !_redo.empty();
}

void Acceptor::redo()
{
	CommandPtr cmd = _redo.top();

	cmd->execute();

	// Update the stacks.
	_redo.pop();
	_undo.push(cmd);

	qDebug("Redid %s", cmd->name());

	notifyObservers();
}

